// Package pay_api provides error handling for payment API operations.
package pay_api

import (
	"fmt"
)

// Error codes for payment API operations
const (
	ErrInvalidConfig    = "INVALID_CONFIG"
	ErrConnectionFailed = "CONNECTION_FAILED"
	ErrAPIError         = "API_ERROR"
	ErrResponseParsing  = "RESPONSE_PARSING"
	ErrInvalidRequest   = "INVALID_REQUEST"
	ErrAuthFailed       = "AUTH_FAILED"
	ErrOrderNotFound    = "ORDER_NOT_FOUND"
	ErrRefundFailed     = "REFUND_FAILED"
	ErrPaymentFailed    = "PAYMENT_FAILED"
)

// PayAPIError represents a payment API specific error
type PayAPIError struct {
	Code    string `json:"code"`
	Message string `json:"message"`
	Details string `json:"details,omitempty"`
}

// Error implements the error interface
func (e *PayAPIError) Error() string {
	if e.Details != "" {
		return fmt.Sprintf("[%s] %s: %s", e.Code, e.Message, e.Details)
	}
	return fmt.Sprintf("[%s] %s", e.Code, e.Message)
}

// NewPayAPIError creates a new PayAPIError
func NewPayAPIError(code, message string) *PayAPIError {
	return &PayAPIError{
		Code:    code,
		Message: message,
	}
}

// NewPayAPIErrorWithDetails creates a new PayAPIError with details
func NewPayAPIErrorWithDetails(code, message, details string) *PayAPIError {
	return &PayAPIError{
		Code:    code,
		Message: message,
		Details: details,
	}
}

// IsPayAPIError checks if an error is a PayAPIError
func IsPayAPIError(err error) bool {
	_, ok := err.(*PayAPIError)
	return ok
}

// GetPayAPIError extracts PayAPIError from error
func GetPayAPIError(err error) *PayAPIError {
	if payErr, ok := err.(*PayAPIError); ok {
		return payErr
	}
	return nil
}